package com.zq.blog_serve.controller.test;

import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HolidayRequest {

    public static ProcessEngine getprocessEngine() {
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://localhost:3306/flowabletest?userSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8")
                .setJdbcUsername("root")
                .setJdbcPassword("root")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver");
//                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        return cfg.buildProcessEngine();
    }

    public static void deploymentRepository() {
        ProcessEngine processEngine = getprocessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .deploy();
    }

    public static void repositoryList() {
        ProcessEngine processEngine = getprocessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
        System.out.println("id|name|deploymentid|key|category|description");
        list.forEach(item -> {
            System.out.println(item.getId() + " |" + item.getName() + " |" + item.getDeploymentId() + "| " + item.getKey() + " |" + item.getCategory() + "| " + item.getDescription());
        });
    }

    public static void startTask(String deploymentid) {
        ProcessEngine processEngine = getprocessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", "员工李四");
        variables.put("nrOfHolidays", "33");
        variables.put("description", "李四想休假33天");
//                runtimeService.startProcessInstanceByKey("holiday-request", variables);
        runtimeService.startProcessInstanceById(deploymentid, variables);
    }

    public static void getTodoTask(String userName) {
        ProcessEngine processEngine = getprocessEngine();

        TaskService taskService = processEngine.getTaskService();

//        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
//        List<Task> tasks = taskService.createTaskQuery().taskCandidateUser(userName).list();
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userName).list();
        System.out.println("You have " + tasks.size() + " tasks:");
        for (int i = 0; i < tasks.size(); i++) {
            Map<String, Object> processVariables = taskService.getVariables(tasks.get(i).getId());
            System.out.println((i + 1) + ") " + tasks.get(i).getId() + "|" + tasks.get(i).getName());
            System.out.println(processVariables.get("employee") + " 想请 " +
                    processVariables.get("nrOfHolidays") + " 天假.");
        }
    }

    public static void getCandidateTask(String userName) {
        ProcessEngine processEngine = getprocessEngine();

        TaskService taskService = processEngine.getTaskService();

//        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
        List<Task> tasks = taskService.createTaskQuery().taskCandidateUser(userName).list();
//        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userName).list();
        System.out.println("You have " + tasks.size() + " tasks:");
        for (int i = 0; i < tasks.size(); i++) {
            Map<String, Object> processVariables = taskService.getVariables(tasks.get(i).getId());
            System.out.println((i + 1) + ") " + tasks.get(i).getId() + "|" + tasks.get(i).getName());
            System.out.println(processVariables.get("employee") + " 想请 " +
                    processVariables.get("nrOfHolidays") + " 天假.");
        }
    }

    public static void getAllTask() {
        ProcessEngine processEngine = getprocessEngine();

        TaskService taskService = processEngine.getTaskService();

//        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
        List<Task> tasks = taskService.createTaskQuery().list();
        System.out.println("You have " + tasks.size() + " tasks:");
        for (int i = 0; i < tasks.size(); i++) {
            Map<String, Object> processVariables = taskService.getVariables(tasks.get(i).getId());
            System.out.println((i + 1) + ") " + tasks.get(i).getId() + "|" + tasks.get(i).getName());
            System.out.println(processVariables.get("employee") + " 想请 " +
                    processVariables.get("nrOfHolidays") + " 天假.");
        }
    }

    public static void completeTask(String taskId) {
        ProcessEngine processEngine = getprocessEngine();

        TaskService taskService = processEngine.getTaskService();
        Map variables = new HashMap<String, Object>();
        variables.put("approved", "接受");
        taskService.complete(taskId, variables);
    }
    public static void claimTask(String taskId,String userName) {
        ProcessEngine processEngine = getprocessEngine();

        TaskService taskService = processEngine.getTaskService();
        taskService.claim(taskId, userName);

    }



    public static void historyList() {
        ProcessEngine processEngine = getprocessEngine();

        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricActivityInstance> activities =
                historyService.createHistoricActivityInstanceQuery()
                        .finished()
                        .taskAssignee("zhangsan")
                        .orderByHistoricActivityInstanceEndTime().asc()
                        .list();

        for (HistoricActivityInstance activity : activities) {
            System.out.println(activity.getActivityId() + activity.getActivityName() + activity.getTaskId() + " took "
                    + activity.getDurationInMillis() + " milliseconds");
        }
    }


    public static void main(String[] args) {
//        deploymentRepository();
//        repositoryList();
//        startTask("holiday-request:4:22504");
//        getCandidateTask("zhangsan");
//        claimTask("30009","zhangsan");
//        getTodoTask("zhangsan");
//        getAllTask();
        completeTask("30009");
//        historyList();
    }
}
